package com.zsinfo.guoss.supplier.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.EditText;


import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.utils.CommentUtil;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class AddAccountActivity extends BaseActivity {

    @BindView(R.id.bank)
    ViewStub bank;
    @BindView(R.id.otherAcc)
    ViewStub other;
//    @BindView(R.id.add_account)
//    Button add_account;

    EditText other_name;

    EditText other_account;
    EditText bank_user_name;
    EditText bank_account;
    EditText bank_name;
    EditText bank_user_phone;


    private String TypeValue = "";


    @OnClick({R.id.add_account})
    void MyClick(View view) {
        switch (view.getId()) {
            case R.id.add_account:
                AddAccount();
                break;
        }
    }


    void AddAliCount(String accNo) {

        GetNetCenter().GetNetCenter(this, RetrofitBuilder.CreateApi().UPDATE_ALI_COUNT(ConstantsCode.update_ali_count, SharedPreferencesUtil.getID(), accNo), new Response() {
            @Override
            public void OK(Object data) {

                showToast("绑定完成");
                finish();
            }

            @Override
            public void Fail(Throwable e) {
                Log.e("Conker", "OK: " + e);
            }

            @Override
            public void complete() {

            }
        });
    }


    void AddAccount() {


        String acctype = "0";
        String accNo = "";
        String name = "";
        String bank = "";
        String phone = "";
        if (TypeValue.equals("银行卡账户")) {
            acctype = "3";
            if (bank_user_name.getText().toString().isEmpty()) {
                CommentUtil.showSingleToast("持卡人姓名不能为空");
                return;
            }
            if (bank_account.getText().toString().isEmpty()) {
                CommentUtil.showSingleToast("银行卡号不能为空");
                return;
            }
            if (bank_account.getText().toString().length() < 19) {
                CommentUtil.showSingleToast("银行卡号格式错误");
                return;
            }
            if (bank_name.getText().toString().isEmpty()) {
                CommentUtil.showSingleToast("开户银行不能为空");
                return;
            }

            if (bank_user_phone.getText().toString().isEmpty()) {
                CommentUtil.showSingleToast("预留手机不能为空");
                return;
            }

            if (bank_user_phone.getText().toString().length() < 11) {
                CommentUtil.showSingleToast("预留手机格式错误");
                return;
            }
            accNo = bank_account.getText().toString();
            bank = bank_name.getText().toString();
            name = bank_user_name.getText().toString();
            phone = bank_user_phone.getText().toString();


            AddBankCount(accNo, name, bank, phone);
        } else {


            if (other_account.getText().toString().isEmpty()) {
                CommentUtil.showSingleToast("账号不能为空");
                return;
            }
            accNo = other_account.getText().toString();

            AddAliCount(accNo);

        }


    }

    private void AddBankCount(String accNo, String name, String bank, String phone) {

        GetNetCenter().GetNetCenter(this, RetrofitBuilder.CreateApi().BANK_CARD_ADD(ConstantsCode.bank_card_add, SharedPreferencesUtil.getID(), name, bank, phone, accNo), new Response() {
            @Override
            public void OK(Object data) {
                showToast("添加成功");
                finish();
            }

            @Override
            public void Fail(Throwable e) {

            }

            @Override
            public void complete() {

            }
        });

    }


    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_add_account;
    }

    @Override
    protected void initView() {
        TypeValue = getIntent().getStringExtra("TypeValue");


        setTitle(TypeValue);

        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if (TypeValue.equals("银行卡账户")) {
            bank.inflate();
            View bankView = findViewById(R.id.account_of_bank);
            bank_user_name = bankView.findViewById(R.id.bank_user_name);
            bank_account = bankView.findViewById(R.id.bank_account);
            bank_name = bankView.findViewById(R.id.bank_name);
            bank_user_phone = bankView.findViewById(R.id.bank_user_phone);
        } else {
            other.inflate();
            View view = findViewById(R.id.account_of_other);
            other_account = view.findViewById(R.id.other_account);
            other_account.setText(getIntent().getStringExtra("TypeCount"));

        }


    }

    @Override
    protected void initNetData() {

    }

    @Override
    protected void customDestroy() {

    }
}
